package com.itheima.service.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.domain.Student;
import com.itheima.domain.User;
import com.itheima.mapper.UserMapper;
import com.itheima.service.UserService;
import com.itheima.utils.MapperUtils;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

//业务层接口实现类
public class UserServiceImpl implements UserService {
    @Override
    public boolean login(String username, String password) {
        //获取sqlSession
        SqlSession sqlSession = MapperUtils.getSqlSession();
        //创建映射对象
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        //调用方法
        User user = mapper.login(username, password);
        //释放资源
        sqlSession.close();
        //判断user是否存在
        if(user != null){
            return true;
        }else {
            return false;
        }
    }

    @Override
    //查询并分页
    public Page selectByPage(int currentPage, int pageSize) {
        //Page page = null;
        List<Student> list = null;
        //获取sqlSession
        SqlSession sqlSession = MapperUtils.getSqlSession();
        //创建映射对象
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        //设置分页参数
        Page<Student> page = PageHelper.startPage(currentPage, pageSize);
        //查询全部
        mapper.findAll();
        //释放资源
        sqlSession.close();

        return page;
    }
}
